define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'coupon_record/index' + location.search,
                    // add_url: 'coupon_record/add',
                    // edit_url: 'coupon_record/edit',
                    del_url: 'coupon_record/del',
                    // multi_url: 'coupon_record/multi',
                    // import_url: 'coupon_record/import',
                    table: 'coupon_record',
                }
            });

            var table = $("#table");

            // 在普通搜索渲染后
            table.on('post-common-search.bs.table', function (event, table) {
              let form = $("form", table.$commonsearch);

             $("input[name='coupon_id']", form).addClass("selectpage").data("source", "coupon/index").data("orderBy","id desc");
              $("input[name='user_id']", form).addClass("selectpage").data("source", "user/index").data("field", "nickname").data("orderBy","id desc").data("formatItem","{nickname}({id})");

              Form.events.cxselect(form);
              Form.events.selectpage(form);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},

                        {field: 'user_id', title: __('User_id'), visible: false},
                        {
                          field: 'user.nickname', title: '用户昵称', operate: false, formatter: function (value, row, index) {
                            return value + '(' + row.user_id + ')';
                          }
                        },
                        {field: 'coupon_id', title: __('Coupon_id'), visible: false},
                        {
                          field: 'name', title: __('Name'), operate: false, formatter: function (value, row, index) {
                            return value + '(' + row.coupon_id + ')';
                          }
                        },
                        {field: 'coupon_type', title: __('Type'), searchList: {"1":'满减券',"2":'注册券'}, formatter: Table.api.formatter.normal},
                        {field: 'money', title: __('Money'), operate:'BETWEEN'},
                        {field: 'min_money', title: __('Min_money'),
                            formatter: function(value, row, index) {
                                // 如果 min_money 为 0，返回 "无门槛"
                                if (value == 0) {
                                    return '无门槛';
                                }
                                // 否则，返回原来的值
                                return value;
                            }
                        },
                        {field: 'expire_time_text', title: __('Expire_time')},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'coupon.name', title: __('Coupon.name'), operate: 'LIKE'},
                        // {field: 'zone.name', title: __('Zone.name'), operate: 'LIKE'},
                        // {field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE'},
                        {
                          field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                          formatter: Table.api.formatter.operate,
                          // buttons: [
                          //   {
                          //     name: 'disable',
                          //     text: '禁用',
                          //     title: '禁用',
                          //     classname: 'btn btn-xs btn-primary btn-ajax btn-disable',
                          //     icon: 'fa fa-times',
                          //     url: 'coupon_record/disable',
                          //     refresh: true,
                          //     visible: function (row) {
                          //       return parseInt(row.status) === 0;
                          //     },
                          //     confirm: '确认禁用该用户已领取的优惠券吗？（禁用后该优惠券记录将不能使用）',
                          //   }
                          // ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
